package com.qfmy.core.security.impl;

import com.alibaba.fastjson.JSON;
import com.qfmy.common.utils.WebUtils;
import com.qfmy.core.pojo.entity.ErrorCode;
import com.qfmy.core.pojo.entity.ResultMessage;
import com.qfmy.core.pojo.exception.CustomException;
import lombok.SneakyThrows;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 描述:
 *      实现授权失败接口，对授权失败进行返回
 * @author zhengjt
 * @date 2022-11-01 15:03:32
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        ResultMessage resultMessage = new ResultMessage(false,ErrorCode.USER_NEVER_PERMISSIONS.getCode(),ErrorCode.USER_NEVER_PERMISSIONS.getMessage());
        String json = JSON.toJSONString(resultMessage);
        WebUtils.renderString(httpServletResponse, json);
    }
}
